package com.seven.springfox.plugin.enums.plugin;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ReflectionUtils;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;

import java.lang.reflect.Field;

/**
 * @author shenjianeng
 * @date 2020/4/15
 */
@SuppressWarnings(value = "all")
public class LongToStringModelPropertyBuilderPlugin implements ModelPropertyBuilderPlugin {

    @Override
    public void apply(ModelPropertyContext context) {
        context.getBeanPropertyDefinition()
            .ifPresent(beanPropertyDefinition -> {
                final Class<?> fieldType = beanPropertyDefinition.getField().getRawType();
                addDescForLongId(context, fieldType);
            });
    }

    @Override
    public boolean supports(DocumentationType delimiter) {
        return true;
    }

    private void addDescForLongId(ModelPropertyContext context, Class<?> fieldType) {
        context.getBeanPropertyDefinition()
            .ifPresent(beanPropertyDefinition -> {
                String fieldName = beanPropertyDefinition.getName();
                Class<?> declaringClass = beanPropertyDefinition.getField().getDeclaringClass();
                Field field = ReflectionUtils.findField(declaringClass, fieldName);
                JsonSerialize annotation = AnnotationUtils.findAnnotation(field, JsonSerialize.class);
                if (annotation != null && ToStringSerializer.class.equals(annotation.using())) {
                    context.getBuilder().type(context.getResolver().resolve(String.class));
                }
            });
    }

}
